package viewPackage;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
@SuppressWarnings("serial")
public class IetsSelecteren extends JDialog {
	private JList listBox;
	private DefaultListModel listModel;
	private JButton terugButton, selecteerButton;
	private JScrollPane listScroller;
	private Box totaalBox = Box.createVerticalBox();
	private Container contentPane = getContentPane();
	private JPanel p1 = new JPanel();
	private boolean okPressed = false,cancelPressed = false;
	private JFrame parent;
	private String naam;
	private JLabel titelTekst;

	public IetsSelecteren(JFrame parent, String naam) {
		super(parent, "Selecteer " + naam, true);
		this.parent = parent;
		this.naam = naam;
		listModel = new DefaultListModel();
		listBox = new JList(listModel);
		Box buttonBox = Box.createHorizontalBox();

		listBox.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listBox.setLayoutOrientation(JList.VERTICAL);
		listBox.setVisibleRowCount(-1);

		JScrollPane listScroller = new JScrollPane(listBox);
		listScroller.setPreferredSize(new Dimension(200, 80));

		Font titelFont = new Font("Dialog", 1, 20);
		titelTekst = new JLabel(naam + " selecteren");
		titelTekst.setFont(titelFont);
		
		selecteerButton = new JButton("Selecteer " + naam);
		terugButton = new JButton("Terug");
		buttonBox.add(selecteerButton);
		buttonBox.add(terugButton);
		
		titelTekst.setAlignmentX((Component.LEFT_ALIGNMENT));
		listScroller.setAlignmentX((Component.LEFT_ALIGNMENT));
		buttonBox.setAlignmentX((Component.LEFT_ALIGNMENT));
		
		
		totaalBox.add(titelTekst);
		totaalBox.add(Box.createVerticalStrut(20));
		totaalBox.add(listScroller);
		totaalBox.add(Box.createVerticalStrut(20));
		totaalBox.add(buttonBox);
		contentPane.add(totaalBox);
	}

	public void setListener(ActionListener listener) {
		selecteerButton.addActionListener(listener);
		terugButton.addActionListener(listener);
	}

	public void lijstElementToevoegen(String element) {
		listModel.addElement(element);
		listBox.validate();
		contentPane.validate();
	}

	public void leegLijst() {
		listModel.clear();
	}

	public JButton getTerugButton() {
		return terugButton;
	}

	public JButton getSelecteerButton() {
		return selecteerButton;
	}

	public JList getListBox() {
		return listBox;
	}

	public Object showDialog(ArrayList items) {
		Object item = null;
		for (Object kl : items) {
			lijstElementToevoegen(kl.toString());
		}
		setVisible(true);
		if (okPressed) {
			if (listBox.getSelectedIndex() != -1) {
				item = items.get(listBox.getSelectedIndex());
			} else {
				item = new Integer(-1);
			}
			
		}
		if(cancelPressed){
			item = null;
		}
		
		return item;
	}

	public void setPressed() {
		okPressed = true;
	}
	public void setCancel(){
		cancelPressed=true;
	}

}
